package com.yonyou.gmmc.dealerApp.api.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.dto.search.DealerAppSearchDto;
import org.marketing.common.dto.testdrivingAm.TestDrivingAmDto;
import org.marketing.common.dto.testdrivingAm.TestDrivingRoutesDto;
import org.marketing.common.dto.testdrivingAm.TestdrivingAmFrecordDto;
import org.marketing.common.exception.ServiceAppException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.dealerApp.api.dao.TtTestdriveAppointmentMapper;
import com.yonyou.gmmc.dealerApp.api.entity.TestdrivingAmPO;
import com.yonyou.gmmc.dealerApp.api.service.impl.TestdrivingAmService;

@Service
@Transactional
public class CallTestDrivingService {
	private Logger  logger = Logger.getLogger(RepairBookingService.class);
	
	@Autowired
    private TtTestdriveAppointmentMapper testdrivingAmDao;

	/**
	 * 更新试乘试驾信息(抢客户)
	 * @param testDrivingAmDto
	 */
	public void updateTestdrivingAm4rush(TestDrivingAmDto testDrivingAmDto) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
            TestdrivingAmPO testdrivingAmPO = new TestdrivingAmPO();

            testDrivingAmDto.setUpdateDate(new Date());

            BeanUtils.copyProperties(testDrivingAmDto, testdrivingAmPO);

            testdrivingAmDao.updateTestdrivingAm4rush(testdrivingAmPO);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceAppException(e.getMessage(), e);
        }
        resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
        resultMap.put(ResultMsgConstants.ERR_MSG, "");

//        return resultMap;
	}
	
	@Autowired
    private TestdrivingAmService testdriveAmService;

    /**
    *
    * @author LiuJun
    * 试驾预约保存
    * @date 2016年11月7日
    * @param testDrivingAmDto
    * @return
    * @throws Exception
    */
//    @RequestMapping(method = RequestMethod.POST,consumes="application/json;charset=UTF-8",produces = "application/json;charset=UTF-8")
    public Map<String,Object> saveTestDrivingAppointment(TestDrivingAmDto testDrivingAmDto) throws Exception {
        
        testDrivingAmDto = testdriveAmService.saveTestDrivingAppointment(testDrivingAmDto);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        if(testDrivingAmDto!=null){
            resultMap.put("result", "success");
            resultMap.put("appointmentId", testDrivingAmDto.getTestdriveAppointmentId());
        }
        return resultMap;
    }
    
    /**
     * 
    * 取得服务顾问试驾预约的信息
    * @author WangHuicheng
    * @date 2016年11月21日
    * @param dealerCode            经销商代码
    * @param salesConsultantId     服务顾问ID
    * @param status                状态
    * @param startDate             预约到店时间区间（FROM/END）
    * @return
     */
//    @RequestMapping(value="/getTestDriverInfo",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
//    @ResponseBody
    public List<TestDrivingAmDto> getTestDriverInfo(String dealerCode, Integer salesConsultantId, Integer status,
    												String startDateFrom,
                                                    String startDateEnd){
        List<TestDrivingAmDto> list = testdriveAmService.getTestDriverInfo(dealerCode,salesConsultantId,status,startDateFrom,startDateEnd);
        return list;
    }
   /**
    *
    * @author LiuJun
    * 更新试驾预约状态
    * @date 2016年11月21日
    * @param testdrivingAmFrecordDto
    * @return
    * @throws Exception
    */
//    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public Map<String,Object> updateTestDrivingAppointStatus(TestdrivingAmFrecordDto testdrivingAmFrecordDto) throws Exception {
        testdriveAmService.updateTestDrivingAppointStatus(testdrivingAmFrecordDto);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
        resultMap.put(ResultMsgConstants.ERR_MSG, "");
        return resultMap;
    }
    
    
    /**
    *
    * @author LiuJun
    * 保存试驾预约跟进记录
    * @date 2016年11月21日
    * @param testDrivingAmDto
    * @return
    * @throws Exception
    */
//    @RequestMapping(value = "/saveFollowRecords", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<String, Object> saveTestDrivingAppointFrecord(TestdrivingAmFrecordDto testdrivingAmFrecordDto) throws Exception {

        testdrivingAmFrecordDto = testdriveAmService.saveTestDrivingAppointFrecord(testdrivingAmFrecordDto);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
        resultMap.put("tlTestdriverRecodeId", testdrivingAmFrecordDto.getTlTestdriverRecodeId());
        resultMap.put(ResultMsgConstants.ERR_MSG, "");
        return resultMap;
    }

    /**
     * @author QianKun 试驾查询列表
     * @date 2016年11月16日
     * @param potentialUserId
     * @return
     * @throws Exception
     */
//    @RequestMapping(method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public List<TestDrivingAmDto> getTestDrivingData(Long potentialUserId,String phone,String submitDateFrom,
    		String submitDateEnd,String startDateFrom,String startDateEnd,String appointmentStatus) throws Exception {
        List<TestDrivingAmDto> dtoList = testdriveAmService.getTestDrivingData(potentialUserId,phone,submitDateFrom,submitDateEnd,startDateFrom,startDateEnd,appointmentStatus);
        return dtoList;
    }
    
    /**
     * @author QianKun 试驾查询详情
     * @date 2016年11月15日
     * @param testdriveAppointmentId
     * @return
     * @throws Exception
     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public List<TestDrivingAmDto> getTestDrivingDataById(String testdriveAppointmentId) throws Exception {
        List<TestDrivingAmDto> dtoList = testdriveAmService.getTestDrivingDataById(testdriveAppointmentId);
        return dtoList;
    }


    /**
     * 
    * DealerApp端检索试乘试驾预约信息
    * @author WangHuicheng
    * @date 2016年11月24日
    * @param testdrivingAmFrecordDto
    * @return
    * @throws Exception
     */
//    @RequestMapping(value = "/getTestDriverInfoList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    @ResponseBody
    public  Map<String, Object> getTestDriverInfoList(DealerAppSearchDto dealerAppSearchDto) throws Exception {
        
        List<TestDrivingAmDto> list  = testdriveAmService.getTestDriverInfoList(dealerAppSearchDto);
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
        resultMap.put(ResultMsgConstants.ERR_MSG, "");
        resultMap.put("testDriver", list);
        
        return resultMap;
    
    }
    
    
    /**
     * 试驾线路
     * 
     * @author WangHuicheng
     * @date 2016年11月30日
     * @param testDrivingRoutesDto
     * @return
     * @throws Exception
     */
//    @RequestMapping(value = "/getTestDriverRoutes", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    @ResponseBody
    public Map<String, Object> getTestDriverRoutes(TestDrivingRoutesDto testDrivingRoutesDto) throws Exception {

        List<TestDrivingRoutesDto> list = testdriveAmService.getTestDriverRoutes(testDrivingRoutesDto);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (null != list && !list.isEmpty()) {
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
            resultMap.put(ResultMsgConstants.ERR_MSG, "");
        } else {
            resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
            resultMap.put(ResultMsgConstants.ERR_MSG, "没有驾驶线路");
        }

        resultMap.put("driverRoutes", list);

        return resultMap;

    }
    
    /**
    *
    * @author LiuJun
    * 获取试驾路线
    * @date 2016年12月30日
    * @param dealerCode
    * @return
    * @throws Exception
    */
//    @RequestMapping(value="/routes",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public List<TestDrivingRoutesDto> getTestDrivingRoutes(String dealerCode) throws Exception {
        return testdriveAmService.getTestDrivingRoutes(dealerCode);
    }

}
